package com.cszjzx.member.service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cszjzx.member.dao.JdbcHelper;
import com.cszjzx.member.dao.MemberDAOImpl;
import com.cszjzx.member.dao.TempleMemberDAO;
import com.cszjzx.member.dao.TempleMemberDAOImpl;
import com.cszjzx.member.po.Member;
import com.cszjzx.member.po.Page;
import com.cszjzx.member.po.TempleMember;

public class TempleMemberServiceImpl implements TempleMemberService {
	private TempleMemberDAO templeMemberDao;
	public TempleMemberServiceImpl() {
		try {
			templeMemberDao = new TempleMemberDAOImpl();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unused")
	@Override
	public boolean addTempleMember(String[] fieldsName, String[] fieldsValue, String idcard, int checkcondition,
			int masterid, int process, int operationType) {
		TempleMember templeMember = new TempleMember();
		Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
 		templeMember.setSubmitTime(dt);
		templeMember.setCheckcondition(checkcondition);
		templeMember.setOperationType(operationType);
		templeMember.setMasterid(masterid);
		if(process == -1 || process == 1){
			Member m = new Member();
			m.setValue("process", Integer.toString(process));
			for(int i=0;i<fieldsName.length;i++){
				m.setValue(fieldsName[i], fieldsValue[i]);
			}
			templeMember.setMember(m);
		}
		else{
			Member m = new MemberServiceImpl().getMemberByIdcard(idcard);
			System.out.println("idcard:"+idcard+"\t"+"checkcondition:"+checkcondition);
			for(int i=0;i<fieldsName.length;i++){
				m.setValue(fieldsName[i], fieldsValue[i]);
			}
			m.setValue("process", Integer.toString(process));
			if(m == null){
				return false;
			}
			templeMember.setMember(m);
		}
		try {
			templeMemberDao.add(templeMember);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	public void addDeletedTempleMember(String idcard, int masterid){
		TempleMember templeMember = new TempleMember();
		Date dt=new Date();
		templeMember.setSubmitTime(dt);
		Member m = new MemberServiceImpl().getMemberByIdcard(idcard);
		templeMember.setMember(m);
		templeMember.setCheckcondition(1);
		templeMember.setOperationType(2);
		templeMember.setMasterid(masterid);
		JdbcHelper helper;
		try {
			helper = new JdbcHelper();
			String sql="delete from member where idcard = ?";
			Object[] args={idcard};
			helper.executeUpdate(sql, args);
			String sql1="delete from templemember where idcard = ?";
			Object[] args1={idcard};
			helper.executeUpdate(sql1, args1);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void addTempleMembers(List<Member> membersList,String process,int masterid){
		List<TempleMember> templeMembers = new ArrayList<TempleMember>();
		int operationType,p;
		Date dt=new Date();
		p = Integer.parseInt(process);
		if(p == -1 || p == 1){
			operationType = 0;
		}
		else {
			operationType = 1;
		}
		for(int i=0;i<membersList.size();i++){
			TempleMember templeMember = new TempleMember();
			templeMember.setMasterid(masterid);
			templeMember.setMember(membersList.get(i));
			templeMember.setSubmitTime(dt);
			templeMember.setCheckcondition(1);
			templeMember.setOperationType(operationType);
			templeMembers.add(templeMember);
			
			try {
				templeMemberDao.add(templeMember);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	@Override
	public Page getPage(int checkcondition,int beginPage){
		Page page = new Page();
		try {
			List<TempleMember> templeMembers = templeMemberDao.findByCheckCondition(checkcondition);
			List<TempleMember> t = new ArrayList<TempleMember>();
			int everyPage = page.getEveryPage();
			for(int i=(beginPage-1)*everyPage,j=0;j<everyPage&&i<templeMembers.size();j++,i++){
				TempleMember tm = templeMembers.get(i);
				int o = tm.getOperationType();
				
				int p = Integer.parseInt(tm.getMember().getValue("process"));
				if(o == 0 ){
					if(p == -1){
						tm.setDetails("老团员添加");
					}
					else if(p == 1){
						tm.setDetails("新团员添加");
					}
					else{
						tm.setDetails("该团员进阶");
					}
				}
				else if(o == 1 ){
					Member member;
					String idcard = tm.getMember().getValue("idcard");
					List<Member> list = new MemberDAOImpl().find("idcard", idcard);
					if(list == null || list.size() == 0){
						System.out.println("0");
						member = null;
					}
					else{
						member = list.get(0);
					}
					
					if(Integer.parseInt(member.getValue("process")) != p){
						tm.setDetails("该团员进阶");
					}
					else{
						tm.setDetails("该团员信息有修改");
					}
				}
				else {
					tm.setDetails("删除该团员");
				}
				t.add(tm);
			}
			page.setList(t);
			page.setBeginPage(beginPage);
			page.setEveryPage(everyPage);
			int totalCount = templeMembers.size();
			page.setTotalCount(totalCount);
			int totalPage = (totalCount%everyPage==0)?(totalCount/everyPage):((totalCount/everyPage)+1);
			page.setTotalPage(totalPage);
			page.setHasNextPage(beginPage>=totalCount);
			page.setHasPrePage(beginPage<=1);
		} catch (ClassNotFoundException | SQLException | IOException e) {
			e.printStackTrace();
		}
		return page;
	}
	@Override
	public Page getPage(int id,int checkcondition,int beginPage){
		Page page = new Page();
		try {
			List<TempleMember> templeMembers = templeMemberDao.findByCheckConditionAndMasterid(id, checkcondition);
			List<TempleMember> t = new ArrayList<TempleMember>();
			int everyPage = page.getEveryPage();
			for(int i=(beginPage-1)*everyPage,j=0;j<everyPage&&i<templeMembers.size();j++,i++){
				TempleMember tm = templeMembers.get(i);
				int o = tm.getOperationType();
				int p = Integer.parseInt(tm.getMember().getValue("process"));
				if(o == 0 ){
					if(p == -1){
						tm.setDetails("老团员添加");
					}
					if(p == 1){
						tm.setDetails("新团员添加");
					}
				}
				else if(o == 1 ){
					String idcard = tm.getMember().getValue("idcard");
					Member member = new MemberDAOImpl().find("idcard", idcard).get(0);
					if(Integer.parseInt(member.getValue("process")) != p){
						tm.setDetails("该团员进阶");
					}
					else{
						tm.setDetails("该团员信息有修改");
					}
				}
				else {
					tm.setDetails("删除该团员");
				}
				t.add(tm);
			}
			page.setList(t);
			page.setBeginPage(beginPage);
			page.setEveryPage(everyPage);
			int totalCount = templeMembers.size();
			page.setTotalCount(totalCount);
			int totalPage = (totalCount%everyPage==0)?(totalCount/everyPage):((totalCount/everyPage)+1);
			page.setTotalPage(totalPage);
			page.setHasNextPage(beginPage>=totalCount);
			page.setHasPrePage(beginPage<=1);
		} catch (ClassNotFoundException | SQLException | IOException e) {
			e.printStackTrace();
		}
		return page;
	}
	@Override
	public boolean updateTempleMember(String idcard,int checkcondition){
		try {
			System.out.println("checkcondition:"+checkcondition);
			TempleMember templeMember = templeMemberDao.findByIdcard(idcard);
			templeMember.setCheckcondition(checkcondition);
			if(templeMember.getMember().getValue("process").equals("1")){
				templeMember.setOperationType(0);
			} else {
				templeMember.setOperationType(1);
			}
			templeMemberDao.update(templeMember);
			List<TempleMember> templeMembers = new ArrayList<TempleMember>();
			templeMembers.add(templeMember);
			templeMemberDao.update(templeMembers);
		} catch (ClassNotFoundException | SQLException | IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	@Override
	public TempleMember getByIdcard(String idcard){
		try {
			return templeMemberDao.findByIdcard(idcard);
		} catch (ClassNotFoundException | SQLException | IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	@Override
	public int getByMasteridAndCheckcondition(int masterid, int checkcondition) {
		List<TempleMember> templeMembers = null;
		try {
			templeMembers = templeMemberDao.findByCheckConditionAndMasterid(masterid, checkcondition);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return templeMembers.size();
	}
	@Override
	public int getByCheckcondition(int checkcondition) {
		List<TempleMember> templeMembers = null;
		try {
			templeMembers = templeMemberDao.findByCheckCondition(checkcondition);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return templeMembers.size();
	}

}
